package cn.org.xiaosheng.resttemplate.utils;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.protocol.HTTP;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author XiaoSheng
 * @date 2023-11-19
 * @dec 使用RestTemplate的Get请求工具类
 */
@Component
public class GetUtils {

    @Resource
    private RestTemplate restTemplate;


    /**
     * 通过GET请求调用第三方平台的API
     *
     * @param url  请求接口路径
     * @param params 请求参数集合
     * @return
     */
    public ResponseEntity<JSONObject> getRequest(String url,
                                                 Map<String, Object> headers,
                                                 Map<String, Object> params
                                                 ) {
        return requestObject(url, HttpMethod.GET, params, headers);
    }

    /**
     * 通过GET请求调用第三方平台的API
     *
     * @param url  请求接口路径
     * @param params 请求参数集合
     * @return
     */
    public JSONObject getRequest(String url,
                                 Map<String, Object> params) {
        // 1. 设置请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        HttpEntity<Object> entity = new HttpEntity<>(params, httpHeaders);
        JSONObject forObject = restTemplate.getForObject(url, JSONObject.class, params);
        return forObject;
    }

    /**
     *
     * @param url
     * url 写成 "http://localhost:8080/testGet2?name={name}&age={age}"; 后面参数标明, 使用{}与集合中的参数名对应并进行参数绑定
     * @param params
     * @return
     */
    public JSONObject getRequestByUrl(String url,
                                      Map<String, Object> params) {
        return restTemplate.getForObject(url, JSONObject.class, params);
    }



    /**
     * 通过GET请求调用第三方平台的API
     *
     * @param url  请求接口路径
     * @param params 请求参数集合
     * exchange方式适用于get, post请求, 需指定具体的请求方法
     * @return
     */
    public  ResponseEntity<JSONObject> requestObject(String url,
                                                        HttpMethod method,
                                                        Map<String, Object> params,
                                                        Map<String, Object> headers) {
        // 1. 设置请求头
        HttpHeaders httpHeaders = new HttpHeaders();
        if (CollUtil.isNotEmpty(headers)) {
            for (String key : headers.keySet()) {
                httpHeaders.set(key, headers.get(key).toString());
            }
        }
        System.out.println(httpHeaders);
        HttpEntity<Object> entity = new HttpEntity<>(httpHeaders);
        ResponseEntity<JSONObject> exchange = restTemplate.exchange(url, method, entity, JSONObject.class, params);
        return exchange;
    }

}
